package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SyEmp;
import cn.com.bdqn.entity.SyMeterType;
import cn.com.bdqn.service.SyEmpService;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/syEmp")
@Api(tags = "员工管理接口")
public class SyEmpController {

    @Autowired
    private SyEmpService syEmpService;

    @Operation(summary = "查询所有员工信息")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(SyEmp syEmp, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<SyEmp> syEmps = syEmpService.queryByLike(syEmp);
            PageInfo pageInfo = new PageInfo(syEmps,3);
            return ReturnData.ok().put("syEmps",syEmps).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("查询所有员工信息数据库异常");
        }
    }

    @Operation(summary = "根据ID查询单个员工信息")
    @PostMapping("/queryById")
    public ReturnData queryById(Long id){
        try {
            SyEmp syEmp = syEmpService.queryById(id);
            return ReturnData.ok().put("syEmp",syEmp);
        }catch (Exception e){
            return ReturnData.currError("根据ID查询单个员工信息数据库异常");
        }
    }

    @Operation(summary = "根据ID删除员工信息")
    @PostMapping("/delSyEmp")
    public ReturnData delSyEmp(Long id){
        int result = syEmpService.delSyEmp(id);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("删除员工信息数据库异常");
        }
    }

    @Operation(summary = "新增员工信息")
    @PostMapping("/addSyEmp")
    public ReturnData addSyEmp(SyEmp syEmp){
        int result = syEmpService.addSyEmp(syEmp);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增员工信息数据库异常");
        }
    }

    @Operation(summary = "修改员工信息")
    @PostMapping("/updateSyEmp")
    public ReturnData updateSyEmp(SyEmp syEmp){
        int result = syEmpService.updateSyEmp(syEmp);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改员工信息数据库异常");
        }
    }
}
